<template>
  <div class="basic-info-container">
    <!-- 表单标题 -->
    <div class="form-header">
      <div class="header-icon">
        <Icon icon="ep:edit" />
      </div>
      <div class="header-content">
        <h3 class="form-title">基本信息编辑</h3>
        <p class="form-subtitle">更新您的个人基本信息</p>
      </div>
    </div>

    <!-- 表单内容 -->
    <div class="form-content">
      <Form ref="formRef" :rules="rules" :schema="schema" :label-width="120" class="enhanced-form">
        <template #sex="form">
          <div class="radio-group-wrapper">
            <el-radio-group v-model="form['sex']" class="custom-radio-group">
              <div class="radio-item">
                <el-radio :label="1" class="custom-radio">
                  <div class="radio-content">
                    <Icon icon="ep:male" class="gender-icon male-icon" />
                    <span>{{ t('profile.user.man') }}</span>
                  </div>
                </el-radio>
              </div>
              <div class="radio-item">
                <el-radio :label="2" class="custom-radio">
                  <div class="radio-content">
                    <Icon icon="ep:female" class="gender-icon female-icon" />
                    <span>{{ t('profile.user.woman') }}</span>
                  </div>
                </el-radio>
              </div>
            </el-radio-group>
          </div>
        </template>
      </Form>
    </div>

    <!-- 操作按钮 -->
    <div class="form-actions">
      <div class="action-buttons">
        <el-button
          type="primary"
          size="large"
          @click="submit()"
          class="save-btn"
          :loading="loading"
        >
          <Icon icon="ep:check" class="btn-icon" />
          {{ t('common.save') }}
        </el-button>
        <el-button size="large" @click="init()" class="reset-btn">
          <Icon icon="ep:refresh" class="btn-icon" />
          {{ t('common.reset') }}
        </el-button>
      </div>
      <div class="action-tips">
        <Icon icon="ep:info-filled" class="tip-icon" />
        <span>修改信息后请点击保存按钮确认更改</span>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import type { FormRules } from 'element-plus'
import { FormSchema } from '@/types/form'
import type { FormExpose } from '@/components/Form'
import {
  getUserProfile,
  updateUserProfile,
  UserProfileUpdateReqVO
} from '@/api/system/user/profile'
import { useUserStore } from '@/store/modules/user'
import UserSocial from './UserSocial.vue'

defineOptions({ name: 'BasicInfo' })

const { t } = useI18n()
const message = useMessage() // 消息弹窗
const userStore = useUserStore()
const loading = ref(false)

// 表单校验
const rules = reactive<FormRules>({
  nickname: [{ required: true, message: t('profile.rules.nickname'), trigger: 'blur' }],
  email: [
    { required: true, message: t('profile.rules.mail'), trigger: 'blur' },
    {
      type: 'email',
      message: t('profile.rules.truemail'),
      trigger: ['blur', 'change']
    }
  ],
  mobile: [
    { required: true, message: t('profile.rules.phone'), trigger: 'blur' },
    {
      pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
      message: t('profile.rules.truephone'),
      trigger: 'blur'
    }
  ]
})

const schema = reactive<FormSchema[]>([
  {
    field: 'nickname',
    label: t('profile.user.nickname'),
    component: 'Input',
    componentProps: {
      placeholder: '请输入昵称',
      clearable: true,
      prefixIcon: 'ep:user'
    }
  },
  {
    field: 'mobile',
    label: t('profile.user.mobile'),
    component: 'Input',
    componentProps: {
      placeholder: '请输入手机号码',
      clearable: true,
      prefixIcon: 'ep:phone'
    }
  },
  {
    field: 'email',
    label: t('profile.user.email'),
    component: 'Input',
    componentProps: {
      placeholder: '请输入邮箱地址',
      clearable: true,
      prefixIcon: 'ep:message'
    }
  },
  {
    field: 'sex',
    label: t('profile.user.sex'),
    component: 'InputNumber',
    value: 0
  }
])

const formRef = ref<FormExpose>() // 表单 Ref

const submit = () => {
  const elForm = unref(formRef)?.getElFormRef()
  if (!elForm) return
  elForm.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const data = unref(formRef)?.formModel as UserProfileUpdateReqVO
        await updateUserProfile(data)
        message.success(t('common.updateSuccess'))
        const profile = await init()
        userStore.setUserNicknameAction(profile.nickname)
      } catch (error) {
        console.error('更新失败:', error)
      } finally {
        loading.value = false
      }
    }
  })
}

const init = async () => {
  const res = await getUserProfile()
  unref(formRef)?.setValues(res)
  return res
}

onMounted(async () => {
  await init()
})
</script>

<style scoped>
.basic-info-container {
  background: linear-gradient(135deg, #f8fafc 0%, #ffffff 100%);
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(0, 82, 204, 0.08);
  max-width: 600px;
  margin: 0 auto;
}

/* 表单头部 */
.form-header {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 32px;
  padding-bottom: 20px;
  border-bottom: 2px solid rgba(0, 82, 204, 0.1);
  position: relative;
}

.form-header::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 60px;
  height: 2px;
  background: linear-gradient(90deg, #0052cc 0%, #1d4ed8 100%);
  border-radius: 1px;
}

.header-icon {
  width: 48px;
  height: 48px;
  background: linear-gradient(135deg, #0052cc 0%, #1d4ed8 100%);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 20px;
  box-shadow: 0 4px 12px rgba(0, 82, 204, 0.3);
}

.header-content {
  flex: 1;
}

.form-title {
  font-size: 20px;
  font-weight: 600;
  color: #1f2937;
  margin: 0 0 4px 0;
}

.form-subtitle {
  font-size: 14px;
  color: #6b7280;
  margin: 0;
}

/* 表单内容 */
.form-content {
  margin-bottom: 32px;
}

.enhanced-form {
  :deep(.el-form-item) {
    margin-bottom: 24px;
  }

  :deep(.el-form-item__label) {
    font-weight: 500;
    color: #374151;
    font-size: 14px;
  }

  :deep(.el-input) {
    .el-input__wrapper {
      border-radius: 8px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
      border: 1px solid #e5e7eb;
      transition: all 0.2s ease;
    }

    .el-input__wrapper:hover {
      border-color: #0052cc;
      box-shadow: 0 2px 12px rgba(0, 82, 204, 0.15);
    }

    .el-input__wrapper.is-focus {
      border-color: #0052cc;
      box-shadow: 0 0 0 3px rgba(0, 82, 204, 0.1);
    }
  }
}

/* 性别选择 */
.radio-group-wrapper {
  padding: 8px 0;
}

.custom-radio-group {
  display: flex;
  gap: 16px;
}

.radio-item {
  flex: 1;
}

.custom-radio {
  width: 100%;
  margin: 0;

  :deep(.el-radio__input) {
    display: none;
  }

  :deep(.el-radio__label) {
    padding: 0;
    width: 100%;
  }
}

.radio-content {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 8px 12px;
  border: 2px solid #e5e7eb;
  border-radius: 12px;
  background: #ffffff;
  cursor: pointer;
  transition: all 0.2s ease;
  font-weight: 500;
  color: #6b7280;
}

.radio-content:hover {
  border-color: #0052cc;
  background: rgba(0, 82, 204, 0.02);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
}

.custom-radio.is-checked .radio-content {
  border-color: #0052cc;
  background: linear-gradient(135deg, rgba(0, 82, 204, 0.1) 0%, rgba(0, 82, 204, 0.05) 100%);
  color: #0052cc;
  box-shadow: 0 4px 16px rgba(0, 82, 204, 0.2);
}

.gender-icon {
  font-size: 18px;
}

.male-icon {
  color: #3b82f6;
}

.female-icon {
  color: #ec4899;
}

.custom-radio.is-checked .male-icon {
  color: #0052cc;
}

.custom-radio.is-checked .female-icon {
  color: #0052cc;
}

/* 操作按钮区域 */
.form-actions {
  text-align: center;
}

.action-buttons {
  display: flex;
  gap: 16px;
  justify-content: center;
  margin-bottom: 16px;
}

.save-btn {
  background: linear-gradient(135deg, #0052cc 0%, #1d4ed8 100%);
  border: none;
  border-radius: 8px;
  padding: 12px 32px;
  font-weight: 500;
  transition: all 0.2s ease;
  box-shadow: 0 4px 12px rgba(0, 82, 204, 0.3);
}

.save-btn:hover {
  background: linear-gradient(135deg, #1d4ed8 0%, #1e40af 100%);
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(0, 82, 204, 0.4);
}

.reset-btn {
  border: 2px solid #e5e7eb;
  border-radius: 8px;
  padding: 12px 32px;
  font-weight: 500;
  color: #6b7280;
  background: #ffffff;
  transition: all 0.2s ease;
}

.reset-btn:hover {
  border-color: #9ca3af;
  color: #374151;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.btn-icon {
  margin-right: 6px;
  font-size: 16px;
}

.action-tips {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-size: 13px;
  color: #9ca3af;
  background: rgba(0, 82, 204, 0.05);
  padding: 8px 16px;
  border-radius: 8px;
  border: 1px solid rgba(0, 82, 204, 0.1);
}

.tip-icon {
  color: #0052cc;
  font-size: 14px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .basic-info-container {
    padding: 16px;
    margin: 0 16px;
  }

  .form-header {
    flex-direction: column;
    text-align: center;
    gap: 12px;
  }

  .custom-radio-group {
    flex-direction: column;
    gap: 12px;
  }

  .action-buttons {
    flex-direction: column;
    align-items: center;
  }

  .save-btn,
  .reset-btn {
    width: 100%;
    max-width: 200px;
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .basic-info-container {
    background: linear-gradient(135deg, #1f2937 0%, #111827 100%);
    border-color: rgba(0, 82, 204, 0.2);
  }

  .form-title {
    color: #f9fafb;
  }

  .form-subtitle {
    color: #9ca3af;
  }

  .enhanced-form {
    :deep(.el-form-item__label) {
      color: #e5e7eb;
    }
  }

  .radio-content {
    background: #374151;
    border-color: #4b5563;
    color: #d1d5db;
  }

  .radio-content:hover {
    background: rgba(0, 82, 204, 0.1);
  }

  .reset-btn {
    background: #374151;
    border-color: #4b5563;
    color: #d1d5db;
  }

  .action-tips {
    background: rgba(0, 82, 204, 0.1);
    border-color: rgba(0, 82, 204, 0.2);
    color: #d1d5db;
  }
}
</style>
